from PySide2 import QtWidgets, QtCore
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QTreeWidgetItemIterator

from binding import template_bind, original_bind, mapping_bind, template_group_bind, check_bind
import share
from binding.show_bind import show_table


class bind:
    def __init__(self):

        self.ui = share.SI.main.ui

        self.ui.menu_treeWidget.clicked.connect(lambda :self.menu_fun())
        self.ui.menu_treeWidget.setRootIsDecorated(False);

        # 首页
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/首页.png"));

        # 参数设置
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/导入.png"));

        # 参数设置
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/模板.png"));

        # 参数设置
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/公式.png"));

        # 参数设置
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/映射.png"));

        # 输出表查看
        item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        item_0.setIcon(0,QIcon("./static/img/映射.png"));

        # # 参数设置
        # item_0 = QtWidgets.QTreeWidgetItem(self.ui.menu_treeWidget)
        # item_0.setIcon(0,QIcon("./static/img/检查.png"));

        self.retranslateUi()

        menu_treeWidget = self.ui.menu_treeWidget

        # 首页按钮绑定
        self.ui.home_button_1.clicked.connect(lambda :self.menu_fun("数据导入",1))
        self.ui.home_button_2.clicked.connect(lambda :self.menu_fun("模板管理",2))
        self.ui.home_button_3.clicked.connect(lambda :self.menu_fun("映射管理",3))
        self.ui.home_button_4.clicked.connect(lambda :self.menu_fun("分组管理",4))

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.ui.menu_treeWidget.header().setVisible(False)
        self.ui.menu_treeWidget.topLevelItem(0).setText(0, _translate("Form", "首页"))
        self.ui.menu_treeWidget.topLevelItem(1).setText(0, _translate("Form", "数据导入"))
        self.ui.menu_treeWidget.topLevelItem(2).setText(0, _translate("Form", "模板管理"))
        self.ui.menu_treeWidget.topLevelItem(3).setText(0, _translate("Form", "映射管理"))
        self.ui.menu_treeWidget.topLevelItem(4).setText(0, _translate("Form", "分组管理"))
        self.ui.menu_treeWidget.topLevelItem(5).setText(0, _translate("Form", "输出表查看"))
        # self.ui.menu_treeWidget.topLevelItem(3).setText(0, _translate("Form", "检查"))

    # 菜单点击事件绑定
    def menu_fun(self,text=None,flag=None):
        if text == None:
            item = self.ui.menu_treeWidget.currentItem()
            text = item.text(0)

        menu_treeWidget = self.ui.menu_treeWidget

        if text == "模板管理":               #模板管理
            #     重新刷新模板
            template_bind.bind().findAll()
            self.ui.stackedWidget.setCurrentIndex(3)
        elif text == "数据导入":             #数据导入
            original_bind.bind().findAll()
            self.ui.stackedWidget.setCurrentIndex(0)

        elif text == "映射管理":             #公式编辑
            mapping_bind.bind().findMapping()
            self.ui.stackedWidget.setCurrentIndex(4)
        elif text == "首页":             #首页
            self.ui.stackedWidget.setCurrentIndex(6)
        elif text == "分组管理":             #分组管理
            self.ui.stackedWidget.setCurrentIndex(7)
            # 渲染分组数据
            template_group_bind.group_data_bind()
            template_group_bind.unit_data_bind()
            template_group_bind.original_group_data_bind()
        elif text == "检查":             #检查
            self.ui.stackedWidget.setCurrentIndex(8)
            # 检查窗口初始化
            check_bind.bind().dataUpdate()
        elif text == "输出表查看":
            self.ui.stackedWidget.setCurrentIndex(10)
        #     输出表表头初始化
            show = show_table()
            show.findAll()

        if flag != None:
            menu_treeWidget.setCurrentItem(menu_treeWidget.topLevelItem(flag))